package com.home.qh.webapp.controller;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.home.qh.model.TransCtrType;
import com.home.qh.service.TransCtrTypeManager;

@Controller
@RequestMapping("/admin/transCtrTypeForm.*")
public class TransCtrTypeFormController extends BaseFormController {
	private transient final Log log = LogFactory.getLog(TransCtrTypeFormController.class);
	private TransCtrTypeManager mgr;
    
    @Autowired
    private ApplicationContext context;

    @Autowired
    public void setTransCtrTypeManager(TransCtrTypeManager transCtrTypeManager) {
        this.mgr = transCtrTypeManager;
    }	
        
    public TransCtrTypeFormController() {
        setCancelView("redirect:/admin/transCtrTypeList.html");
        setSuccessView("redirect:/admin/transCtrTypeList.html");
    }


	@ModelAttribute
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST})
    protected ModelAndView showForm(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ModelAndView view = new ModelAndView("admin/transCtrTypeForm");
        
        log.debug("entering 'showForm' method...");
        if (!isFormSubmission(request)) {
            String transCtrTypeId = request.getParameter("id"); 
            TransCtrType transCtrType = null;
	        if (transCtrTypeId == null || isAdd(request)) {
	        	transCtrType = new TransCtrType();
	        } else if (!StringUtils.isBlank(transCtrTypeId)) {
	        	transCtrType = mgr.get(Long.parseLong(transCtrTypeId));
	        }
	        view.addObject("transCtrType", transCtrType);
        }
        
    	return view;
    }
    
    @RequestMapping(method = RequestMethod.POST)
    public String onSubmit(TransCtrType transCtrType, BindingResult errors, HttpServletRequest request,
                           HttpServletResponse response)
            throws Exception {
        if (request.getParameter("cancel") != null) {
            if (!StringUtils.equals(request.getParameter("from"), "list")) {
                return getCancelView();
            } else {
                return getSuccessView();
            }
        }


        log.debug("entering 'onSubmit' method...");

        Locale locale = request.getLocale();

        if (request.getParameter("delete") != null) {
        	mgr.remove(transCtrType.getId());
            return getSuccessView();
        } else {
        	if (transCtrType == null)
        		transCtrType = new TransCtrType();   
        	log.debug("Save transCtrType");
        	
        	mgr.save(transCtrType);
            return getSuccessView();        	
        }
    }

    private boolean isFormSubmission(HttpServletRequest request) {
        return request.getMethod().equalsIgnoreCase("post");
    }

    protected boolean isAdd(HttpServletRequest request) {
        String method = request.getParameter("method");
        return (method != null && method.equalsIgnoreCase("add"));
    }  

}
